package com.weixin.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.weixin.config.WeiXinProperties;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zjd
 * @Title:
 * @Description:
 * @date 2021/4/1213:57
 */
@Api("企业微信认证")
@RestController
@RequestMapping("/v1.0/wei-xin/cgi-bin")
public class ConnectionController {
    @Autowired
    private RestTemplate restTemplate;
    @Resource
    private ObjectMapper objectMapper;


    @ApiOperation("获取access_token  type(获取不同的access_token) 0获取应用的CorpSecret 1获取通讯录的CorpSecret ")
    @GetMapping("token")
    public ResponseEntity getAccessToken(Integer type,HttpSession session) throws IOException {
        String secret= type != 0 ?WeiXinProperties.getAddressBookSecret() : WeiXinProperties.getCorpSecret();
        StringBuilder urlBuilder=new StringBuilder(WeiXinProperties.getUrl());
        urlBuilder.append("cgi-bin/gettoken?");
        urlBuilder.append("corpid="+WeiXinProperties.getCorpId());
        urlBuilder.append("&corpsecret="+secret);
        String result = restTemplate.getForObject(urlBuilder.toString(), String.class);
        Map<String, String> map = objectMapper.readValue(result, Map.class);
        for (Map.Entry<String, String> entry : map.entrySet()) {
            if (entry.getKey().equals("errcode")){
                Integer integer = Integer.valueOf(String.valueOf(map.get("errcode")));
                if (integer==0){
                    String sessionKey= type != 0 ?"access_token_addressBook" :"access_token_application";
                    session.setAttribute(sessionKey,map.get("access_token"));
                    return new ResponseEntity(map.get("access_token"),HttpStatus.OK);
                }
            }
        }
        return new ResponseEntity(map,HttpStatus.INTERNAL_SERVER_ERROR);
    }



    @ApiOperation("获取企业微信API域名IP段")
    @GetMapping("/getApiDomainIp")
    public ResponseEntity getApiDomainIp(HttpSession session) throws IOException {
        StringBuilder urlBuilder=new StringBuilder(WeiXinProperties.getUrl());
        urlBuilder.append("cgi-bin/get_api_domain_ip?");
        String access_token =(String) session.getAttribute("access_token_application");
        urlBuilder.append("access_token="+access_token);
        String result = restTemplate.getForObject(urlBuilder.toString(), String.class);
        Map<String, Object> map = objectMapper.readValue(result, HashMap.class);
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            if (entry.getKey().equals("errcode")){
                Integer integer = Integer.valueOf(String.valueOf(map.get("errcode")));
                if (integer==0){
                    return new ResponseEntity(map.get("ip_list"),HttpStatus.OK);
                }
            }
        }
        return new ResponseEntity(map,HttpStatus.INTERNAL_SERVER_ERROR);
    }
}
